#!/usr/bin/python3
#coding=utf8

import os
from threading import Thread
from imp import find_module,load_module,acquire_lock,release_lock
from lib.log import Logger

NOT_IMPORT = ['__init__.py']
class BasePluginsManage(object):
    def __init__(self, userdir ='./plugins/'):
        self.plugin_path = userdir
        self.plugins = []
        self.load_plugins()

    def load_plugins(self):
        for folder in os.listdir(self.plugin_path):
            self.add_plugin(folder)

    def add_plugin(self,plugin):
        if plugin not in self.plugins and \
        plugin not in NOT_IMPORT and \
        plugin.endswith(".py"):
            self.plugins.append(plugin[:-3])

    def add_plugins(self,plugins):
        for plugin in plugins:
            self.add_plugin(plugin)

    def del_plugin(self,plugin):
        if plugin in self.plugins:
            self.plugins.remove(plugin)

    def del_plugins(self,plugins):
        for plugin in plugins:
            self.del_plugin(plugin)

class PluginsManage(object):
    def __init__(self,userdir='./plugins/'):
        bpm = BasePluginsManage(userdir)
        self.plugins = bpm.plugins

#########################################################
class BaseConfig(object):
    thread_num = 10       #线程数
    http_timeout = 10     #http超时
    sock_timeout = 10     #sock超时
    type = 2              #0-其它 1-url 2-ip
    name = 'plugin_name'
    help = '漏洞简要描述'
    desc = '漏洞详细描述及解决方案'
    version = '0.1'

class BasePOC(object):
    def __init__(self,CONFIG):
        self.log = Logger(CONFIG.name)

class URLPOC(BasePOC):
    def __init__(self,url,CONFIG):
        BasePOC.__init__(self,CONFIG)

class IPPOC(BasePOC):
    def __init__(self,host,port,CONFIG):
        BasePOC.__init__(self,CONFIG)

class BaseEXP(BasePoc):
    pass


#########################################################
class BaseThread(object):
    def __init__(self,queue,num_thread=10):
        self.Queue = queue
        self.num_thread = num_thread

    def todo(self,data):
        #time.sleep(0.0001)
        pass

    def run(self):
        while self.Queue.qsize() > 0:
            data = self.Queue.get()
            self.todo(data)

    def start(self,daemon=True):
        for _ in range(self.num_thread):
            t=Thread(target = self.run)
            if daemon:
                t.setDaemon(daemon)
            t.start()

###########################################################
class DBHelper(object):
    def __init__(self, dbname, rewrite = False):
        self.db = getttr(models,dbname)
        self.rewrite = rewrite #是否覆盖
        print(self.dbname)

    def __call__(self,func):
        def handler(*args):
            result = func(*args)
            if result:
                print(result)
                #self.db.get_or_create(**data)
        return handler
###########################################################
class Update(object):
    pass

############################################################
if __name__=='__main__':
    print(
        BasePluginsManage('../').plugins
    )